package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TProjectMeasures;
import com.blt.govdata.service.ITProjectMeasuresService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tProjectMeasures")
//todo 将所有的XXX修改为真实值
public class TProjectMeasuresController extends BaseController {
    @Autowired
    ITProjectMeasuresService service;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 根据项目ID查询项目下一步措施
     */

    @RequestMapping(value = "/selectPageByPid", params = {"Pid"})
    public Map<String, Object> selectPageByPid(Integer Pid, Page page) {
        Page<TProjectMeasures> result = service.selectPage(page, BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", Pid)
                .and("is_delete = {0}", Constants.UN_DELETED)
                .and("examine_state = {0}", Constants.AUDITED_PASS));
        logger.info("分页根据项目ID查询项目下一步措施");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增项目措施
     */
    @RequestMapping(value = "/addProjectMeasures")
    public Map<String, Object> addProjectMeasures(TProjectMeasures tProjectMeasures) {
        if (null == tProjectMeasures || tProjectMeasures.getpId() == null)
            return ImmutableMap.of("status", "false", "data", false);
        boolean result = this.service.insert(tProjectMeasures);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑项目措施
     */
    @RequestMapping(value = "/editProjectMeasures")
    public Map<String, Object> editProjectMeasures(TProjectMeasures tProjectMeasures) {
        if (null == tProjectMeasures || tProjectMeasures.getId() == null)
            return ImmutableMap.of("status", "false", "data", false);
        boolean result = this.service.updateById(tProjectMeasures);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除项目措施
     */
    @RequestMapping(value = "/deleteProjectMeasures", params = {"id"})
    public Map<String, Object> deleteProjectMeasures(Integer id) {
        TProjectMeasures tProjectMeasures = BeanUtils.instantiate(TProjectMeasures.class);
        tProjectMeasures.setId(id);
        tProjectMeasures.setIsDelete(0);
        boolean result = this.service.updateById(tProjectMeasures);
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 根据ID查询项目措施
     */
    @RequestMapping(value = "/selectProjectMeasuresById", params = {"id"})
    public Map<String, Object> selectProjectMeasuresById(Integer id) {
        return ImmutableMap.of("status", "true", "data", this.service.selectById(id));
    }

}
